package com.niuke;

import java.util.*;

/**
 * @ClassName : HJ68
 * @Author : kele
 * @Date: 2023/4/2 13:59
 * @Description :
 */
public class HJ68 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        while (in.hasNext()) {

            int num = Integer.parseInt(in.nextLine());
            String flag = in.nextLine();
            TreeMap<Integer, String> map = new TreeMap<>();
            if (flag.equals("0")) {
                map = new TreeMap<>((o1, o2) -> o2 - o1);
            }

            for (int i = 0; i < num; i++) {
                String[] line = in.nextLine().split("\\s+");
                String name = line[0];
                int score = Integer.parseInt(line[1]);
                map.merge(score, name, (a, b) -> a + "," + b);
            }

            for (Map.Entry<Integer, String> entry : map.entrySet()) {
                String[] split = entry.getValue().split(",");
                for (String s : split) {
                    System.out.println(entry.getKey() + " " + s);
                }

            }

        }


    }

}
